package world.criver.mm_vworld_server.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import world.criver.crc.CRC;
import world.criver.mm_vworld_server.config.conConfig;
import world.criver.mm_vworld_server.data.daSave;
import world.criver.mm_vworld_server.data.daServer;
import world.criver.mm_vworld_server.enums.enumKey;
import world.criver.mm_vworld_server.function.funAdminMessage;
import world.criver.mm_vworld_server.function.funAdminVerifyCode;
import world.criver.mm_vworld_server.function.funSave;
import world.criver.mm_vworld_server.function.funServer;

import java.net.InetAddress;

@Service
public class serAdminServer {
    @Autowired
    private funAdminVerifyCode funAdminVerifyCode;
    @Autowired
    private funAdminMessage funAdminMessage;
    @Autowired
    private funServer funServer;
    @Autowired
    private funSave funSave;
    @Autowired
    private conConfig conConfig;

    private ObjectMapper om=new ObjectMapper();
    private CRC crc=new CRC();

    public ObjectNode start(JsonNode info){
        if(!info.has(enumKey.verifyCode.name()))return funAdminMessage.error("缺少VerifyCode");
        String code=info.get(enumKey.verifyCode.name()).asText();
        if(!conConfig.isTest())if(!funAdminVerifyCode.test(code))return funAdminMessage.error("已在其他地方登陆");
        if(daServer.server!=null&&!daServer.server.isClosed())return funAdminMessage.error("服务器已经启动");
        if(!funServer.start())return funAdminMessage.error("服务器启动失败");
        if(!funSave.load()){
            funServer.stop();
            return funAdminMessage.error("存档加载失败");
        }
        try {
            daServer.id = crc.encrypt(conConfig.getEncryptKey(), InetAddress.getLocalHost().getHostAddress() + ":" + conConfig.getServerPort());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return state(info);
    }
    public ObjectNode stop(JsonNode info){
        if(!info.has(enumKey.verifyCode.name()))return funAdminMessage.error("缺少VerifyCode");
        String code=info.get(enumKey.verifyCode.name()).asText();
        if(!conConfig.isTest())if(!funAdminVerifyCode.test(code))return funAdminMessage.error("已在其他地方登陆");
        if(daServer.server==null&& daServer.server.isClosed())return funAdminMessage.error("服务器未启动");
        if(!funSave.save())return funAdminMessage.error("存档保存失败");
        if(!funServer.stop())return funAdminMessage.error("服务器停止失败");
        daServer.id="********";
        return state(info);
    }
    public ObjectNode state(JsonNode info){
        if(!info.has(enumKey.verifyCode.name()))return funAdminMessage.error("缺少VerifyCode");
        String code=info.get(enumKey.verifyCode.name()).asText();
        if(!conConfig.isTest())if(!funAdminVerifyCode.test(code))return funAdminMessage.error("已在其他地方登陆");
        ObjectNode on=om.createObjectNode();
        on.put(enumKey.status.name(), true);
        on.put(enumKey.run.name(), daServer.server!=null&&!daServer.server.isClosed());
        on.put(enumKey.nowPlayerNum.name(), daServer.clients.size());
        on.put(enumKey.maxPlayerNum.name(), daSave.properties.getMaxOnlineNum());
        on.put(enumKey.allPlayerNum.name(), daSave.players.size());
        on.put(enumKey.serverID.name(), daServer.id);
        on.put(enumKey.serverVersion.name(),daSave.version);
        on.put(enumKey.timestamp.name(), System.currentTimeMillis());
        return on;
    }
}
